#!/bin/bash

# 工作路径
g_workPath=$(cd `dirname \$0`; pwd -P)
cd $g_workPath

. $g_workPath/.env
. $g_workPath/func.sh

streamConf="
stream { 
    include /etc/nginx/stream/*.conf;
}
"

# 创建 nginx 配置文件及目录
mkConfPath(){
	checkFileExist share/conf/nginx.conf
	if [ $? == 1 ]
	then
		echo "conf file exist, whether init [y/n]"
		read input
		if [ "y" != "$input" ]
		then
			echo "Passing initialization"
			return
		fi
	fi

	docker run -d --name nginx_cp ${image}

	sleep 2s
 
	mkdir -p share
	rm -fr share/*

	docker cp nginx_cp:/etc/nginx share/conf
	docker cp nginx_cp:/usr/share/nginx/html share/html
	# docker cp nginx_cp:/var/log/nginx logs	
	
	docker rm -f nginx_cp

	mkdir -p share/www \
		share/logs \
		share/conf/stream \
		share/conf/certificate \
		share/conf/conf.d/proxy_pass  

	printf "%s\n" "$streamConf" >> share/conf/nginx.conf
}

# 创建一个公有网络
mkNetworkPublic net-public
mkNetworkPrivate net-private

echo "----- create path data/ conf/ html/ logs/ ------"
mkConfPath

setEnv "path" "$g_workPath" .env
set "uid" "`id -u`" .env
set "gid" "`id -g`" .env

